package com.fangzhi.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fangzhi.mobel.AdminAccountInfo;
import com.fangzhi.mobel.AdminAccountRoleDeptRelation;
import com.fangzhi.mobel.DeptBaseInfo;
import com.fangzhi.mobel.RoleBaseInfo;
import com.fangzhi.mobel.enums.GlobalCodeEnum;
import com.fangzhi.mobel.io.CreateUserIo;
import com.fangzhi.mobel.vo.DeptListVo;
import com.fangzhi.mobel.vo.LoginVo;
import com.fangzhi.mobel.vo.RoleListVo;
import com.fangzhi.service.AdminAccountInfoService;
import com.fangzhi.service.AdminAccountRoleDeptRelationService;
import com.fangzhi.service.DeptBaseInfoService;
import com.fangzhi.service.RoleBaseInfoService;
import com.fangzhi.utils.GlobalParam;
import com.fangzhi.utils.ResponseResult;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private DeptBaseInfoService deptBaseInfoService;
    @Autowired
    private RoleBaseInfoService roleBaseInfoService;
    @Autowired
    private AdminAccountInfoService adminAccountInfoService;
    @Autowired
    private AdminAccountRoleDeptRelationService adminAccountRoleDeptRelationService;

    /**
     * 创建员工账号
     *
     * @return
     */
    @PostMapping("/createOrUpdateUser")
    public ResponseResult createOrUpdateUser(@RequestBody CreateUserIo io) {
        if (null != io.getAvatar())
            io.setAvatar(io.getAvatar().replace(GlobalParam.IMAGE_URL, ""));
        // 验证账号是否是管理员权限
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        AdminAccountRoleDeptRelation one = adminAccountRoleDeptRelationService.getOne(Wrappers.<AdminAccountRoleDeptRelation>lambdaQuery()
                .eq(AdminAccountRoleDeptRelation::getAdminAccounntId, loginVo.getId()).in(AdminAccountRoleDeptRelation::getRoleId, Arrays.asList(1, 2)));
        if (null == one)
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);

        if (io.getId() > 0)
            // 修改用户
            return adminAccountInfoService.updateUser(io);
        return adminAccountInfoService.saveUser(io);
    }

    /**
     * 员工列表
     *
     * @param roleId
     * @param deptId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/userList")
    public ResponseResult userList(@RequestParam(defaultValue = "0") Integer roleId
            , @RequestParam(defaultValue = "0") Integer deptId
            , @RequestParam(defaultValue = "0") Integer accountId
            , @RequestParam(defaultValue = "1") Integer pageNo
            , @RequestParam(defaultValue = "20") Integer pageSize) {
        List<RoleListVo> roleListVos = roleBaseInfoService.roleList(roleId);
        List<DeptListVo> deptListVos = deptBaseInfoService.deptList(deptId);
        if (roleListVos.isEmpty() || deptListVos.isEmpty())
            return ResponseResult.systemException(GlobalCodeEnum.GL_FAIL_9997);

        List<Long> roleIds = roleListVos.stream().map(T -> T.getId()).collect(Collectors.toList());
        List<Long> deptIds = deptListVos.stream().map(T -> T.getId()).collect(Collectors.toList());
        LoginVo loginVo = GlobalParam.LOGIN_ACCOUNT_INFO.get();
        if (loginVo.getRoleId() == 3 || loginVo.getRoleId() == 6)
            accountId = loginVo.getId().intValue();
        return adminAccountInfoService.userList(roleIds, deptIds, accountId, pageNo, pageSize);
    }
}
